/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyDate;

import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.util.*;
import java.io.*;
import sUtils.*;

class MyDateView extends Canvas {
    MyDate parent;
    Calendar c = Calendar.getInstance();
    Vector days = new Vector();
    ImgRes res = new ImgRes();
    Menu menu = new Menu();
    int current = 60, downKey = 0;
    int y0 = 0, oy = 0, ch = 45;
    long downTime = 0, lastTime = 0;
    public boolean slider = false;

    public MyDateView( MyDate p )
    {
        parent = p;
        Date d = new Date();
        long time = System.currentTimeMillis();
        time /= 24*60*60000;
        time -= 60;
        y0 = -58*ch;
        time *= 24*60*60000;
        time -= TimeZone.getDefault().getRawOffset();
        d.setTime(time);
        c.setTime(d);
        for ( int i=0; i<365; i++, time+=24*60*60000 )
        {
            d.setTime(time);
            c.setTime(d);
            days.addElement( new MyDateDay(c,time,current==i,parent.pim) );
        }
        writeInfoHTML( (MyDateDay)days.elementAt(current) );

        menu.init(getWidth(), getHeight(), 0, true);
        menu.add( Lang.word("Quit","Beenden"), "quit");
        menu.add(null,null);
        menu.add( Lang.word("New Event","Neuer Termin"),"new");
        menu.add( Lang.word("View of Day","Tagesansicht"),"show");
    }

    public void removeEntry( MyDateEntry e )
    {
        for ( int i=0; i<days.size(); i++ )
        {
            MyDateDay day = (MyDateDay) days.elementAt(i);
            day.removeEntry(e);
        }
    }

    public void updateEntry( MyDateEntry e )
    {
        for ( int i=0; i<days.size(); i++ )
        {
            MyDateDay day = (MyDateDay) days.elementAt(i);
            day.updateEntry(e);
        }
    }

    public void addEntry( MyDateEntry e )
    {
        for ( int i=0; i<days.size(); i++ )
        {
            MyDateDay day = (MyDateDay) days.elementAt(i);
            e.addTo(day);
        }
    }

    public void writeStr( DataOutputStream o, String s )
    {
        if (s!=null && s.length()>0) try {
            for ( int i=0; i<s.length(); i++) o.writeByte((byte)(s.charAt(i)));
        } catch( IOException e ) { }
    }

    protected void writeInfoHTML( MyDateDay day )
    {
        if ( day!=null ) try {
            FileConnection fHTML = (FileConnection) Connector.open("file:///Record/mydate.htm");
            if (fHTML.exists()) fHTML.delete(); fHTML.create();
            DataOutputStream html = fHTML.openDataOutputStream();
            if (html!=null)
            {
                writeStr( html, "<html>\r\n" );
           //     <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
                writeStr( html, "<META HTTP-EQUIV=REFRESH CONTENT=5>\r\n");
                writeStr( html, "<head><title>MyDate</title></head>\r\n");
                writeStr( html, "<STYLE type=\"text/css\">\r\n");
                writeStr( html, "BODY { background: white; color: #000080}\r\n");
                writeStr( html, "</STYLE>\r\n");

                writeStr( html, "<body><h1>" );
                writeStr( html, day.getDateStr() );
               // writeStr( html, " "+day.timeStart+" - "+day.timeEnd);
                writeStr( html, "</h1><br>\r\n" );

                if (day.entry!=null)
                for (int i=0;i<day.entry.size();i++ )
                {
                    MyDateEntry e = (MyDateEntry)(day.entry.elementAt(i));
                   writeStr( html, e.getDesc(day.timeStart));
               //     writeStr( html, " "+e.time+" - " +e.end );
                //    if (e.uid!=null) writeStr( html, " = "+e.uid );
                    writeStr( html, "<br>\r\n" );
                }
                writeStr( html, "</body>\r\n");
                writeStr( html, "</html>\r\n");
                html.close();
            }
            fHTML.close();
        } catch( Exception e ) { }

    //    day.save("file:///mmc/mydate2.jpg");
    }



    public MyDateEntry setText( MyDateEntry e, String text, int hh, int mm, int len, int alarm )
    {
        MyDateDay day = getCurrent();
        long time = System.currentTimeMillis()+TimeZone.getDefault().getRawOffset();
        if (e==null)
        {
            e = parent.pim.createEntry(text, (day==null) ? time : day.timeStart, hh, mm, len, alarm);
        }
        else
        {
            parent.pim.editEntry(e.uid, text, (day==null) ? time : day.timeStart, hh, mm, len, alarm);
        }
        return e;
    }

    public void checkDates()
    {
        int h = getHeight();
        int end = (h-y0+ch-1) / ch;
        if (days.size()<=end)
        {
            int start = days.size();
            int i = start;
            MyDateDay prev = (MyDateDay)days.elementAt(i-1);
            long time = prev.timeEnd;
            Date d = new Date();
            while (i<=end)
            {

                d.setTime(time);
                c.setTime(d);
                days.addElement( new MyDateDay(c,time,current==i,parent.pim) );
                time+=24*60*60000;
                i++;
            }
        }
    }

    public void paint(Graphics g)
    {
        int w = getWidth();
        int h = getHeight();


        int start = -y0 / ch;
        int end = (h-y0+ch-1) / ch;
        checkDates();
        int y = y0 + start*ch;
        MyDateDay d = null, cur = null;
        for ( int i=start; i<=end; i++,y+=ch )
        {
            d = (MyDateDay)(days.elementAt(i));
            if (cur==null || current==i) cur=d;
            d.paint(g, 0, y, w, ch, res);
        }

        //if (current>=0 && current<days.size())
        //   cur = (MyDateDay)(days.elementAt(current));

        if (cur!=null)
            res.updateMenu(g, cur.day, cur.month, cur.year);

        menu.paint(g);
    }

    public void close()
    {
        c = null;
        days = null;
    }

    public void scroll( int index )
    {
        y0 += (index*ch);
        repaint();
    }

    public void showMenu()
    {
        menu.show();
        repaint();
    }

    public MyDateDay getCurrent()
    {
        return (current>=0 && current<days.size()) ? (MyDateDay)(days.elementAt(current)) : null;
    }


    public void pointerPressed( int x, int y )
    {
        if (menu.active)
        {
            menu.hit(x, y);
            if (menu.updated) repaint();
        }
        else
        {
            downKey = res.getKey(x,y,getWidth(),getHeight());
            //if (downKey!=0) Display.getDisplay(parent).vibrate(10);

            if (downKey==-1) { scroll(14); }else
            if (downKey==1) { scroll(-14); }else
            if (downKey==2) { y0 = (-(current=60)*ch); repaint(); }else
            {
                if ( (System.currentTimeMillis() - downTime)<300 )
                {
                    int i = (y-y0)/ch;
                    if ( current == i ) {
                        MyDateDay d= (MyDateDay)(days.elementAt(current));
                        //parent.edit();
                        d.show(parent);
                        d.removeCommand(parent.cancel);
                        //d.addCommand(parent.evEdit);
                        d.addCommand(parent.evRemove);
                        d.addCommand(parent.cancel);
                        d.setCommandListener(parent);
                    }

                }
                oy=y;
            }
            downTime = System.currentTimeMillis();
        }
        slider = false;
    }

    public void pointerReleased( int x, int y )
    {
        if (menu.active)
        {
            parent.onMenu(menu.hit(x, y));
            menu.release();
            repaint();
        }
        else
        {
        if ( downKey==0 && (System.currentTimeMillis()-downTime) < 200 && !slider )
        {
            int i = (y-y0)/ch;
            if ( current != i )
            {
                MyDateDay d=null;
                if (current>=0 && current<days.size()) d = (MyDateDay)(days.elementAt(current)); if (d!=null) d.setCurrent(false);
                if (i>=0 && i<days.size())
                {
                    d = (MyDateDay)(days.elementAt(i));
                    if (d!=null) d.setCurrent(true);
                    current = i;
                }
                repaint();
            }

        }
        }
        slider = false;
    }

    public void pointerDragged( int x, int y )
    {
        if (menu.active)
        {
            menu.hit(x, y);
            if (menu.updated) repaint();
        }
        else
        if (downKey==0)
        {
            slider = true;
            int dy = y-oy;
            y0 += dy*3;
            if (y0>0) y0 = 0;
            repaint();
            oy = y;
        }
    }

    public void keyPressed( int key )
    {
       if (key==-1) { scroll(-14); }
       if (key==-2) { scroll(14); }

    }

}
